import { Component, OnInit, Injector, Type, TemplateRef, ViewChild, Input } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { HttpService } from "../../core/service/http.service";
import { Router, NavigationExtras } from "@angular/router";
import {
  AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig,
  SystemFormControl, FieldType, ElementShowType, CustomNavigation, NavigatType, DrillSubjectOutput
} from "../..";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import {
  FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
  FormControl
} from "@angular/forms";
import { SystemFormOutput } from "../../core/model/SystemFormDto";
import { DrillRoleComponent } from './drill-role.component';
import { ClassGetter } from '@angular/compiler/src/output/output_ast';
import { DrillFlowComponent } from './drill-flow.component';
import { map } from '../../../../node_modules/rxjs/operators';



@Component({
  selector: 'app-drill-subject',
  templateUrl: '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.html',
  styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class DrillSubjectComponent extends NgZorroPageBaseComponent implements OnInit {
  constructor(injector: Injector) {
    super(injector,
      new BaseComponentConfig(
        "DrillProject/GetPagerDrillSubject",
        "DrillProject/CreateDrillSubject",
        "DrillProject/UpdateDrillSubject",
        "DrillProject/GetDrillSubject",
        "DrillProject/DeteleDrillSubject",
        "id",
        "DrillSubject",
        "演练主题管理",
        new Array<CustomNavigation>(
          new CustomNavigation('主题角色', '', ElementShowType.TableOperation, 'anticon anticon-bars', NavigatType.Model, 'role'),
          new CustomNavigation(
            '主题流程', './app/drill/flow', ElementShowType.TableOperation, 'anticon anticon-bars', NavigatType.Route, 'flow'),
          new CustomNavigation('案例背景', './app/drill/drillbg', ElementShowType.TableOperation, 'anticon anticon-minus-square-o', NavigatType.Route, 'drilbg'),
          new CustomNavigation('实训指导', './app/drill/drillguide', ElementShowType.TableOperation, 'anticon anticon-bar-chart', NavigatType.Route, 'drilguide'),
          new CustomNavigation('课程资料', './app/drill/drilldata', ElementShowType.TableOperation, 'anticon anticon-credit-card', NavigatType.Route, 'drildata'),
          new CustomNavigation('角色演练模板管理', './app/drill/roletemplate', ElementShowType.TableOperation, 'anticon anticon-credit-card', NavigatType.Route, 'drildata'),
        ),
        'drillProjectId'
      ));
    this.storageName = "Subject";
    this.isNavigation = true;
  }


  ngOnInit() {
    this.isSpinning = true;
    this.route.queryParams
      .pipe(map(params => params['caption']))
      .subscribe(e => this.componentConfig.ModuleCaption = `${this.componentConfig.ModuleCaption}-[${e}]`);
    this.route.queryParamMap
      .pipe(map(params => params.get('id') || null))
      .subscribe(e => {
        this._pKeyFieldValue = Number(e);
        if (!this._pKeyFieldValue) {
          this.message.error('无法确定主键信息!');
        } else {
          this.isSpinning = false;
        }
        this.IniObserver.subscribe(sb => {
          this.getEntityList();
        });
      });




  }

  getNewEntity(): DrillSubjectOutput {
    return new DrillSubjectOutput();
  }

  async navigateCustom(url: string, pid: any) {
    const msgId = this.message.loading('正在获取数据', { nzDuration: 0 }).messageId;
    try {
      const findEntity = await this.httpService.get<DrillSubjectOutput>(`${this.componentConfig.GetSingle}?pid=${this._pKeyFieldValue}&id=${pid}`);
      if (findEntity) {
        const navigationExtras: NavigationExtras = {
          queryParams: { 'id': pid, 'caption': findEntity.drillSubjectName }
        };
        window.localStorage.setItem(this.storageName, JSON.stringify(this.listEntities));
        this.router.navigate([url], navigationExtras);
      } else {
        this.message.error('无法确定表单信息！请刷新列表！');
      }
    } finally {
      this.message.remove(msgId);
    }

  }

  showModalForComponent(entity: DrillSubjectOutput, tag: string) {
    const keyValue = entity.id;
    const component = <Type<any>>(tag === 'role' ? DrillRoleComponent : DrillFlowComponent);
    const subscription = this.nzModalService.create({
      nzTitle: `${entity.drillSubjectName}`,
      nzContent: component,
      nzComponentParams: {
        pKeyFieldValue: keyValue,
        caption: entity.drillSubjectName
      },
      nzFooter: null,
      nzWidth: document.documentElement.clientWidth - 60,
    });
  }
}
